﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kooboo.Connect;
using System.Web.Security;
using Kooboo.Globalization;
using Kooboo.Commerce;


namespace Kooboo.Web.Controllers
{
    public class RegisterController : ControllerBase
    {
        //
        // GET: /Register/

        public ActionResult Index()
        {
            this.BindViewData();

            return View();
        }

        [HttpPost]
        public ActionResult Index(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new Kooboo.Connect.User()
                {
                    Name = model.UserName,
                    Email = model.Email,
                    Membership = new Kooboo.Connect.Membership
                    {
                        Password = model.Password
                    }
                };

                try
                {
                    user.ToCustomer(model.Password);
                    FormsAuthentication.SetAuthCookie(user.Name, false);
                    return RedirectToAction("Complete");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Exception", ex.Message);
                }
            }

            this.BindViewData();
            return View(model);
        }

        public ActionResult Complete()
        {
            this.BindViewData();

            return View();
        }
    }
}
